<?php

class Pages_ajax extends Admin_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->sSkinFile = 'ajax';
	}
	
	public function index()
	{
		return null;
	}
	
	public function get_pages()
	{
		$iPageId = $this->input->post('page_id', true, 0);
		
		$this->load->model('db/views/pages_extended', 'vw_pages_extended');
		$this->vw_pages_extended->page_deleted = 'N';
		$this->vw_pages_extended->page_status = 'LI';
		$this->vw_pages_extended->page_id = $iPageId;
		$aOptions = array();
		$aOptions[] = array('group_by' => array('page_id'));
		
		$qPages = $this->vw_pages_extended->get($aOptions, 1);
		
		$this->aData['qPages'] = $qPages;
		
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/pages/ajax/index', $this->aData, true);
		
		$this->vw_pages_extended->clear();
		
		$this->vw_pages_extended->page_deleted = 'N';
		$this->vw_pages_extended->page_status = 'LI';
		$this->vw_pages_extended->page_parent_id = $iPageId;
		$aOptions = array();
		$aOptions[] = array('where' => array('page_id !=', $iPageId));
		$aOptions[] = array('group_by' => array('page_id'));
		$aOptions[] = array('order_by' => array('page_pos', 'ASC'));
		
		$qPages = $this->vw_pages_extended->get($aOptions);
		
		$this->aData['qPages'] = $qPages;
		
		$sContent .= $this->load->view('platform/'.$this->sPlatform.'/admin/pages/ajax/index', $this->aData, true);

		$sContent = $this->jsonify_string($sContent, false);

		$data['json_encode'] = array('sContent' => $sContent);
		
		$this->load->view('php_json_encode', $data);
	}
	
	

	public function get_parent_page_id()
	{
		$iPageId = $this->input->post('page_id');
		
		$this->load->model('db/page_parent_lookup', 'page_parent_lookup');
		
		$this->page_parent_lookup->page_id = $iPageId;
		
		$qPageParent = $this->page_parent_lookup->get(1);
		
		
		if ($qPageParent->num_rows() === 1)
		{
			$aJsonEncode = array(
						'page_parent_id' => $qPageParent->row(0)->page_parent_id,
						'top_level' => 'N'
			);
			
			if ($qPageParent->row(0)->page_parent_id === $qPageParent->row(0)->page_id)
			{
				$aJsonEncode['top_level'] = 'Y';
			}
			
			$this->aData['json_encode'] = $aJsonEncode;
			
			$this->load->view('php_json_encode', $this->aData);	
		}
		
	}
	
	
	
	public function search_pages()
	{
		$sSearchText = $this->input->post('sSearchText', true, '');
		$sSearchUrl = $this->input->post('sSearchUrl', true, '');
		$sSearchWhere = $this->input->post('sSearchWhere', true, '');
		$iSearchSite = (int) $this->input->post('iSearchSite', true, 0);
		$iSearchLang = (int) $this->input->post('iSearchLang', true, 0);

	
		$this->load->model('db/views/pages_extended_content', 'vw_pages_extended_content');

		
		if ($iSearchSite !== 0)
		{
			$this->vw_pages_extended_content->site_id = $iSearchSite;	
		}
		
		if ($iSearchLang !== 0)
		{
			$this->vw_pages_extended_content->lang_id = $iSearchLang;	
		}
		

		$this->vw_pages_extended_content->page_status = 'LI';
		$this->vw_pages_extended_content->page_deleted = 'N';
		//$this->vw_pages_extended_content->parent_page_status = 'LI';	

		$aOptions = array();
		
		$aOptions[] = array('where' => array('parent_page_status', 'LI', true, 1, 0));
		$aOptions[] = array('or_where' => array('parent_page_status', null, true, 0, 1));
		
		$sFuncCall = 'like';
		
		if ($sSearchText !== '')
		{
			$bSearchTitle = (in_array($sSearchWhere, array('title', '')) === true);
			$bSearchBody = (in_array($sSearchWhere, array('body', '')) === true);
			
			$iStartBrackets = 0;
			
			if (($bSearchTitle === true) && ($bSearchBody === true))
			{
				$iStartBrackets = 1;
			}
			
			$iEndBrackets = $iStartBrackets;
			
			if (in_array($sSearchWhere, array('title', '')) === true)
			{
				$aOptions[] = array($sFuncCall => array(
												'page_title', $sSearchText, 'both', $iStartBrackets, 0 
				));
				
				
				$sFuncCall = 'or_like';
				
				
				$aOptions[] = array($sFuncCall => array(
												'page_menu_title', $sSearchText, 'both', 0, 0 
				));
			}
			
			if (in_array($sSearchWhere, array('body', '')) === true)
			{
				$aOptions[] = array($sFuncCall => array(
												'page_content', $sSearchText, 'both', 0, $iEndBrackets 
				));
				
				$sFuncCall = 'or_like';
					
			}
		}
		
		
		$qPages = $this->vw_pages_extended_content->get($aOptions);
		
		
		
		$this->aData['iDisplayResults'] = (int) $this->input->post('display_results', true, 0);
		$this->aData['iPageNum'] = (int) $this->input->post('page_num', true, 1);
		$this->aData['qPages'] = $qPages;		
		$this->aData['bSearch'] = true;
	
		$this->load->view('platform/'.$this->sPlatform.'/admin/pages/ajax/row_data_json', $this->aData);
		return;
	}
	
	
	public function edit_inplace() 
	{
		$iPageDataId = (int) $this->input->post('iPageDataId', true, 0);
		
		$this->load->model('db/page_content', 'page_content');
		
		$this->page_content->page_data_id = $iPageDataId;
		
		$qPageContent = $this->page_content->get(1);
		

		
		$sContent = '';
		
		if ($qPageContent->num_rows() === 1)
		{
			$sContent = $qPageContent->row(0)->page_content;
		}	
		
		$this->aData['sContent'] = $sContent;
		$this->aData['iPageDataId'] = $iPageDataId;
		
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/pages/edit_inplace', $this->aData, true);

		$sContent = $this->jsonify_string($sContent, false);

		$data['json_encode'] = array('sContent' => $sContent);
		
		$this->load->view('php_json_encode', $data);

	}
	
	


	public function edit_media_props() 
	{
		$iMediaId = $this->input->post('iMediaId', true, 0);
		$iMediaWidth = $this->input->post('iMediaWidth', true, 0);
		$iMediaHeight = $this->input->post('iMediaHeight', true, 0);
		
		$sMediaAlt = $this->input->post('sMediaAlt', true, '');
		$sDimensionType = $this->input->post('sDimensionType', true, 'W');
		
		$this->load->model('db/views/media_by_folders', 'vw_media_by_folders');
		
		$this->vw_media_by_folders->media_id = $iMediaId;
		$this->vw_media_by_folders->dimension_type = $sDimensionType;
		
		
		$qMedia = $this->vw_media_by_folders->get(array(), 1);
		

		if ($qMedia->num_rows() === 0)
		{
			//TODO: throw an error	
		}
		else 
		{
			$this->aData['oMedia'] = $qMedia->row(0);
			$this->aData['iMediaWidth'] = $iMediaWidth;
			$this->aData['iMediaHeight'] = $iMediaHeight;
			$this->aData['sMediaAlt'] = $sMediaAlt;
			
			$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/pages/edit_media_props', $this->aData, true);
			
			$sContent = $this->jsonify_string($sContent, false);
		
			$this->aData['json_encode']['sContent'] = $sContent;
			
			$this->load->view('php_json_encode', $this->aData);
		}	
	}
	
	

	
	public function get_available_gmaps() 
	{
		$iSiteId = (int) $this->input->post('iSiteId', true, 0);	
		
		$this->load->model('db/views/gmaps_by_site', 'vw_gmaps_by_site');
		
		$this->vw_gmaps_by_site->site_id = $iSiteId;
		$this->vw_gmaps_by_site->version_active = 'Y';
		
		
		$qGoogleMaps = $this->vw_gmaps_by_site->get();
		
		$this->aData['qGoogleMaps'] = $qGoogleMaps;
		
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/pages/available_gmaps', $this->aData, true);
		
		$sContent = $this->jsonify_string($sContent, false);
		
		$this->aData['json_encode']['sContent'] = $sContent;
		$this->aData['json_encode']['iGoogleMaps'] = $qGoogleMaps->num_rows();
		
		$this->load->view('php_json_encode', $this->aData);
		
	}	
	
	
	
	public function get_gmap_details()
	{
		$iGmapId = (int) $this->input->post('iGmapId', true, 0);
		
		$this->aData['iGmapId'] = $iGmapId;
		
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/pages/gmap_details', $this->aData, true);
		
		$sContent = $this->jsonify_string($sContent, false);
		
		$this->aData['json_encode']['sContent'] = $sContent;
		
		$this->load->view('php_json_encode', $this->aData);
		
	}
	
	
	
	public function set_page_status()
	{
		
		$iPageId = (int) $this->input->post('iPageId', true, 0);
		$sStatusType = $this->input->post('sStatusType', true, '');
		$sCurrentStatus = $this->input->post('sCurrentStatus', true, '');
		$sRecurse = $this->input->post('sRecurse', true, 'N');
		
		$sNewStatus = ($sCurrentStatus === 'N') ? 'Y' : 'N';
		
		$this->load->model('db/pages_tbl', 'pages');
		
		$this->pages->page_id = $iPageId;
		
		if ($sStatusType === 'archived')
		{
			$this->pages->page_archived = $sNewStatus;
		}
		else 
		{
			$this->pages->page_deleted = $sNewStatus;	
		}
		
		$this->pages->save();
		
		
		if ($sRecurse === 'Y')
		{
			$this->set_child_page_status($iPageId, $sStatusType, $sNewStatus);
			
		}
		

		
		$this->aData['json_encode'] = array('sDone' => 'Y');
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	private function set_child_page_status($iPageParentId, $sStatusType, $sNewStatus)
	{
		$this->load->model('db/page_parent_lookup', 'page_parent_lookup');
		$this->load->model('db/pages_tbl', 'pages');
		
		
		$this->page_parent_lookup->page_parent_id = $iPageParentId;
		
		$qPages = $this->page_parent_lookup->get();
		$iPages = $qPages->num_rows();
		
		
			
		for ($i = 0; $i < $iPages; $i++)
		{
			$this->pages->clear();
			
			$this->pages->page_id = $qPages->row($i)->page_id;
			
			
			if ($sStatusType === 'archived')
			{
				$this->pages->page_archived = $sNewStatus;
			}
			else 
			{
				$this->pages->page_deleted = $sNewStatus;	
			}
			
			$this->pages->save();
			
			$this->set_child_page_status($qPages->row($i)->page_id, $sStatusType, $sNewStatus);
			
		}
		
	}
	
	
	
	public function activate_page()
	{
		$iPageId = (int) $this->input->post('iPageId', true, 0);
		$iPageDataId = (int) $this->input->post('iPageDataId', true, 0);	
		
		$this->load->model('db/views/pages_extended', 'vw_pages_extended');
		$this->load->model('db/page_data', 'page_data');
		
		$this->vw_pages_extended->page_id = $iPageId;
		$this->vw_pages_extended->page_status = 'LI';
		
		$qPage = $this->vw_pages_extended->get(array(), 1);
		
		if ($qPage->num_rows() === 1)
		{
			$this->page_data->page_data_id = $qPage->row(0)->page_data_id;
			$this->page_data->page_status = 'NL';
			
			$this->page_data->save();
		}
		
		$this->page_data->clear();
		
		$this->page_data->page_data_id = $iPageDataId;
		$this->page_data->page_status = 'LI';
		
		$this->page_data->save();
		
		
		
		$this->aData['json_encode'] = array('sDone' => 'Y');
		
		$this->load->view('php_json_encode', $this->aData);
	}
	


	public function save_page() 
	{
		$iPageDataId = (int) $this->input->post('page_data_id', true, 0);
		$iPageParentId = (int) $this->input->post('parent_page_id', true, 0);
		$sPageTitle = $this->input->post('page_title', true, '');
		$sPageContent = $this->input->post('page_content', true, '');
		$sPageName = $this->input->post('page_seo_title', true, null);
		$sPageKeywords = $this->input->post('page_seo_keywords', true, '');
		$sPageDesc = $this->input->post('page_seo_description', true, '');
		$iPageBanner = $this->input->post('page_banner', true, false);
		
		$aComponents_L = $this->input->post('components_L', true, array());
		$aComponents_A = $this->input->post('components_A', true, array());
		$aComponents_B = $this->input->post('components_B', true, array());
		$aComponents_R = $this->input->post('components_R', true, array());
		
		$iPageId = 0;
		$oPageData = false;
		
		$this->db->trans_start();
		
		$this->load->model('db/page_data', 'page_data');
		
		
		if ($iPageDataId !== 0)
		{
			$this->page_data->page_data_id = $iPageDataId;
			
			$qPageData = $this->page_data->get(array(), 1);
			
			if ($qPageData->num_rows() === 1)
			{
				$iPageId = (int) $qPageData->row(0)->page_id;	
				$oPageData = $qPageData->row(0);
			}	
		}
		
		if ($oPageData === false) 
		{
			$oPageData = $this->page_data->get_object();	
		}
		
		
		// so firstly, we need to either update or save the record in the 'pages' table
		$this->load->model('db/pages', 'pages');
		
		// if pageId isn't zero, then we're updating a page
		if ($iPageId !== 0)
		{
			$this->pages->page_id = $iPageId;
		}
		
		$this->pages->page_keywords = $sPageKeywords;
		$this->pages->page_desc = $sPageDesc;
		
		if($iPageId == 1)
		{
			$this->pages->page_name = NULL;
		}
		else if ($sPageName !== null)
		{
			$this->pages->page_name = $sPageName;
		}
		
		$iPageId = $this->pages->save(false);
		
		
		// now check to see if we have a record in page_parent_lookup
		$this->load->model('db/page_parent_lookup', 'page_parent_lookup');
		
		$this->page_parent_lookup->page_id = $iPageId;
		
		$qPageParent = $this->page_parent_lookup->get(1);
		
		// if we've already got a record for this
		if ($qPageParent->num_rows() === 1)
		{
			// just check to see if the new value is different
			if ($qPageParent->row(0)->page_parent_id != $iPageParentId)
			{
				$this->page_parent_lookup->clear();
				$this->page_parent_lookup->lookup_id = $qPageParent->row(0)->lookup_id;
				$this->page_parent_lookup->page_parent_id = $iPageParentId;
				
				$this->page_parent_lookup->save();	
			}
		}
		else 
		{
			// we need to get the maximum position for this pageParentId
			$this->page_parent_lookup->clear();
			$this->page_parent_lookup->page_parent_id = $iPageParentId;

			$iMaxPagePos = (int) $this->page_parent_lookup->get_max('page_pos');
			// increment the value
			$iMaxPagePos++;
			
			$this->page_parent_lookup->page_id = $iPageId;
			$this->page_parent_lookup->page_pos = $iMaxPagePos;
			
			$this->page_parent_lookup->save();
				
		}
		
		$this->page_data->clear();
		$this->page_data->page_id = $iPageId;
		$this->page_data->page_status = 'LI';
		
		$qPageData = $this->page_data->get(array(), 1);
		
		if($qPageData->num_rows() === 1)
		{
		
			$this->page_data->clear();
			$this->page_data->page_data_id = $qPageData->row(0)->page_data_id;
			$this->page_data->page_status = 'NL';
			$this->page_data->save();
	
		}
		
		$sNewPageStatus = 'LI';
		
		// check to see how many records we have in the page_data table for this page
		$this->page_data->clear();
		$this->page_data->page_id = $iPageId;

		$sPageCurrent = 'N';
		
		$iPageCount = $this->page_data->get_count();
		
		// if we've got not pages at the moment, then this MUST be the current page
		if ($iPageCount === 0)
		{
			$sPageCurrent = 'Y';	
		} 
		else if ($sNewPageStatus === 'LI') // if we're publishing, then we must set all current active pages to N
		{
			$sPageCurrent = 'Y';
			
			$this->page_data->page_current = 'Y';
			
			$qGetCurrent = $this->page_data->get();
			
			if ($qGetCurrent->num_rows() > 0)
			{
				foreach ($qGetCurrent->result() as $oCurrent)
				{
					$this->page_data->clear();
					$this->page_data->page_data_id = $oCurrent->page_data_id;
					$this->page_data->page_current = 'N';
					
					$this->page_data->save();	
				}	
			}
		}

		
		
		$this->page_data->clear();
		$this->page_data->page_id = $iPageId;
		$this->page_data->page_menu_title = $sPageTitle;
		$this->page_data->page_title = $sPageTitle;
		$this->page_data->user_id = $this->user->userId;
		$this->page_data->page_epoch = time();
		$this->page_data->page_version = (($oPageData->page_version === '') ? 1 : ((int) $oPageData->page_version + 1));
		$this->page_data->page_status = $sNewPageStatus;
		$this->page_data->page_current = $sPageCurrent;

		$iNewPageDataId = $this->page_data->save();

		
		// now we do the menus
		$this->load->model('db/menus', 'menus');
		$this->load->model('db/page_data_menus', 'page_data_menus');

		$this->menus->menu_active = 'Y';
		
		$qMenus = $this->menus->get();
		$iMenus = $qMenus->num_rows();
		
		$aMenuTypes = array('A', 'H');
		
		for ($i = 0; $i < $iMenus; $i++)
		{

			foreach ($aMenuTypes as $sMenuType)
			{
				//menu_id_$menuId_$menuType
				$sMenu = $this->input->post('menu_id_'.$sMenuType.'_'.$qMenus->row($i)->menu_id, true, 'N');
		
				if ($sMenu === 'Y')
				{
					$this->page_data_menus->clear();
					$this->page_data_menus->page_data_id = $iNewPageDataId;
					$this->page_data_menus->menu_id = $qMenus->row($i)->menu_id;
					$this->page_data_menus->page_data_menu_type = $sMenuType;
					
					$this->page_data_menus->save();
				}
			
			}			
		
		}

		// save content
		$this->load->model('db/page_content', 'page_content');
		
		$this->page_content->page_data_id = $iNewPageDataId;
		$this->page_content->page_content = $sPageContent;
		
		$this->page_content->save();
		
		//components
		$this->load->model('db/page_data_components', 'page_data_components');
		
		$this->page_data_components->page_data_id = $iNewPageDataId;
		
		$this->page_data_components->component_position = 'L';
		foreach($aComponents_L as $iComponentId)
		{
			if($iComponentId > 0)
			{
				$this->page_data_components->component_id = $iComponentId;
				$this->page_data_components->save();
			}
		}
		
		$this->page_data_components->component_position = 'A';
		foreach($aComponents_A as $iComponentId)
		{
			if($iComponentId > 0)
			{
				$this->page_data_components->component_id = $iComponentId;
				$this->page_data_components->save();
			}
		}
		
		$this->page_data_components->component_position = 'B';
		foreach($aComponents_B as $iComponentId)
		{
			if($iComponentId > 0)
			{
				$this->page_data_components->component_id = $iComponentId;
				$this->page_data_components->save();
			}
		}
		
		$this->page_data_components->component_position = 'R';
		foreach($aComponents_R as $iComponentId)
		{
			if($iComponentId > 0)
			{
				$this->page_data_components->component_id = $iComponentId;
				$this->page_data_components->save();
			}
		}
		
		if($iPageBanner > 0)
		{
			//banners
			$this->load->model('db/page_data_banners', 'page_data_banners');
			$this->page_data_banners->page_data_id = $iNewPageDataId;
			$this->page_data_banners->banner_id = $iPageBanner;
			$this->page_data_banners->save();
		}
		
		$aJsonEncode = array();
		
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
		{
		    // generate an error... or use the log_message() function to log your error
			$aJsonEncode['sError'] = 'an error has occured'; // TODO: as DB message record
		} 
		else 
		{
			$aJsonEncode['iPageDataId'] = $iNewPageDataId;
		}
		
		$this->aData['json_encode'] = $aJsonEncode;
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	
	
	
	public function save_content()
	{
		$iPageDataId = (int) $this->input->post('iPageDataId', true, 0);
		$sHtml = $this->input->post('sHtml', true, '');	
		$sPublish = $this->input->post('sPublish', true, 'N');
		
	
		// load page data
		$this->load->model('db/page_data', 'page_data');
		
		$this->page_data->page_data_id = $iPageDataId;
		
		$qPageData = $this->page_data->get(array(), 1);
		
		if ($qPageData->num_rows() === 1)
		{
			$this->db->trans_start();
		
			
			if ($sPublish === 'Y')
			{

				$this->load->model('db/views/pages_extended', 'vw_pages_extended');
				
				$this->vw_pages_extended->page_id = $qPageData->row(0)->page_id;
				$this->vw_pages_extended->page_status = 'LI';
				
				$qPage = $this->vw_pages_extended->get(array(), 1);
				
				if ($qPage->num_rows() === 1)
				{
					$this->page_data->page_data_id = $qPage->row(0)->page_data_id;
					$this->page_data->page_status = 'NL';
					
					$this->page_data->save();
				}				
				
			}
		
			/////////////////////////////////////////
			// now deal with the page data - start //
			/////////////////////////////////////////
			$this->page_data->clear();
			
			
			$this->page_data->page_id = $qPageData->row(0)->page_id;
			$this->page_data->page_menu_title = $qPageData->row(0)->page_menu_title;
			$this->page_data->page_title = $qPageData->row(0)->page_title;
			$this->page_data->user_id = $this->user->userId;
			$this->page_data->page_epoch = time();
			$this->page_data->page_version = ($qPageData->row(0)->page_version + 1);
			$this->page_data->page_status = (($sPublish === 'N') ? 'NL' : 'LI');
			$this->page_data->page_publish_from = $qPageData->row(0)->page_publish_from;
			$this->page_data->page_publish_to = $qPageData->row(0)->page_publish_to;
			
			$iNewPageDataId = $this->page_data->save();
			///////////////////////////////////////
			// now deal with the page data - end //
			///////////////////////////////////////

			
			
			////////////////////////////////////////////
			// now deal with the page content - start //
			////////////////////////////////////////////
			$this->load->model('db/page_content', 'page_content');
			
			$this->page_content->page_data_id = $iNewPageDataId;
			$this->page_content->page_content = $sHtml;
			
			$this->page_content->save();
			//////////////////////////////////////////
			// now deal with the page content - end //
			//////////////////////////////////////////

			
			
			////////////////////////////////////////////
			// now deal with the page banners - start //
			////////////////////////////////////////////
			$this->load->model('db/page_data_banners', 'page_data_banners');
			
			$this->page_data_banners->page_data_id = $iPageDataId;
			
			$qPageBanners = $this->page_data_banners->get();
			$iPageBanners = $qPageBanners->num_rows();
			
			for ($i = 0; $i < $iPageBanners; $i++)
			{
				$this->page_data_banners->clear();
				
				$this->page_data_banners->page_data_id = $iNewPageDataId;
				$this->page_data_banners->banner_id = $qPageBanners->row($i)->banner_id;

				$this->page_data_banners->save();
			
			}
			//////////////////////////////////////////
			// now deal with the page banners - end //
			//////////////////////////////////////////

			
			
			//////////////////////////////////////////
			// now deal with the page menus - start //
			//////////////////////////////////////////
			$this->load->model('db/page_data_menus', 'page_data_menus');
			
			$this->page_data_menus->page_data_id = $iPageDataId;
			
			$qPageMenus = $this->page_data_menus->get();
			$iPageMenus = $qPageMenus->num_rows();
			
			for ($i = 0; $i < $iPageMenus; $i++)
			{
				$this->page_data_menus->clear();
				
				$this->page_data_menus->page_data_id = $iNewPageDataId;
				$this->page_data_menus->menu_id = $qPageMenus->row($i)->menu_id;
				$this->page_data_menus->page_data_menu_type = $qPageMenus->row($i)->page_data_menu_type;

				$this->page_data_menus->save();
			
			}
			////////////////////////////////////////
			// now deal with the page menus - end //
			////////////////////////////////////////

			
			
			///////////////////////////////////////////////
			// now deal with the page spotlights - start //
			///////////////////////////////////////////////
			$this->load->model('db/page_data_spotlights', 'page_data_spotlights');
			
			$this->page_data_spotlights->page_data_id = $iPageDataId;
			
			$qPageSpotlights = $this->page_data_spotlights->get();
			$iPageSpotlights = $qPageSpotlights->num_rows();
			
			for ($i = 0; $i < $iPageSpotlights; $i++)
			{
				$this->page_data_spotlights->clear();
				
				$this->page_data_spotlights->page_data_id = $iNewPageDataId;
				$this->page_data_spotlights->spotlight_id = $qPageSpotlights->row($i)->spotlight_id;

				$this->page_data_spotlights->save();
			
			}
			/////////////////////////////////////////////
			// now deal with the page spotlights - end //
			/////////////////////////////////////////////
			
			
			
			
			
			$aJsonEncode = array();
			$this->db->trans_complete();
			
			
			if ($this->db->trans_status() === FALSE)
			{
			    // generate an error... or use the log_message() function to log your error
				$aJsonEncode['sError'] = 'an error has occured'; // TODO: as DB message record
			} 
			else 
			{
				$aJsonEncode['iPageDataId'] = $iNewPageDataId;
			}
			
			
			$this->aData['json_encode'] = $aJsonEncode;
			
			
			$this->load->view('php_json_encode', $this->aData);
		}
		
			
		
	}
	
	
	
	public function get_page_content()
	{
		$iPageId = (int) $this->input->post('iPageId', true, 0);	
		
		$this->load->model('db/views/pages_extended_content', 'vw_pages_extended_content');
		
		$this->vw_pages_extended_content->page_id = $iPageId;
		$this->vw_pages_extended_content->page_status = 'LI';
		
		$qPageContent = $this->vw_pages_extended_content->get(array(), 1);
		
		$sContent = '';
		
		if ($qPageContent->num_rows() === 1)
		{
			$sContent = $qPageContent->row(0)->page_content;
			
			$sContent = $this->jsonify_string($sContent, false);	
		}
		
		
		$this->aData['json_encode'] = array('sContent' => $sContent);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	
	public function get_page_details() 
	{
		$iPageId = (int) $this->input->post('iPageId', true, 0);	
		
		$this->load->model('db/views/pages_extended', 'vw_pages_extended');
		
		$this->vw_pages_extended->page_id = $iPageId;
		$this->vw_pages_extended->page_status = 'LI';
		
		$qPageContent = $this->vw_pages_extended->get(1);
		
		$oRet = (object) array();
		
		if ($qPageContent->num_rows() === 1)
		{
			$oRet = $qPageContent->row(0);	
		}
		
		
		
		$this->aData['json_encode'] = $oRet;
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	public function get_page_news()
	{
		$iPageDataId = $this->input->post('iPageDataId', true, 0);
		$iOffset = $this->input->post('iOffset', true, 0);
		
		$this->load->library('pages_lib');
		
		$sContent = $this->pages_lib->get_page_news($iPageDataId, $iOffset, true);
		
		$this->aData['json_encode'] = array('sContent' => $sContent);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	public function get_page_tree($iNode = NULL)
	{
		$iNode = $iNode === NULL ? $this->input->get('root') : $iNode;
		
		$this->load->model('db/sites', 'sites');
		
		$qSites = $this->sites->get(1);
		
		$this->load->model('db/views/pages_extended', 'vw_pages_extended');
		
		$aPages = $this->vw_pages_extended->get_page_tree(array(
																'page_name' => NULL,
																'site_id' => $qSites->row(0)->site_id
		));	
		
		$sContent = '['.$this->vw_pages_extended->build_page_tree($aPages, $qSites->row(0), 0, 'page_parent_id', true).']';
		
		//$sContent = preg_replace('/\s\s+/', ' ', $sContent);
		//$sContent = preg_replace('/\r?\n/m', '', $sContent);
		//$sContent = preg_replace('/\t/m', '', $sContent);
		//$sContent = addslashes($sContent);
		
		echo $sContent;
		
		exit;
	}
}

?>